package com.sunstar.cwcamerademo

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext


class TestViewModel private constructor() : ViewModel() {

    var isTester = MutableLiveData(false)
    var type = TesterType.HOT_REBOOT

    companion object {
        val instance by lazy {
            TestViewModel()
        }
    }

    private var _hotRebootTimes = MutableLiveData<Int>(0)
    private var _coldRebootTimes = MutableLiveData(0)
    private var _maxColdRebootTimes = MutableLiveData(2000)
    private var _maxHotRebootTimes = MutableLiveData(10000)

    // 检测测试完成
    suspend fun checkColdTesterComplete(): Boolean {
        if (_coldRebootTimes.value!! >= _maxColdRebootTimes.value!!) {
            stopTester()
        }
        return isTester.value ?: false
    }

    // 检测测试完成
    suspend fun checkHotTesterComplete(): Boolean {
        if (_hotRebootTimes.value!! >= _maxHotRebootTimes.value!!) {
            stopTester()
        }
        return isTester.value ?: false
    }

    suspend fun addTimes() {
        when (type) {
            TesterType.HOT_REBOOT -> addHotRebootTimes()
            TesterType.COLD_REBOOT -> addColdRebootTimes()
        }
    }

    suspend fun getColdNum() {
        var number = withContext(Dispatchers.IO) {
            DSHelper.getData(COLD_NUM)
        }
        withContext(Dispatchers.Main) {
            _coldRebootTimes.value = number
        }

    }

    private suspend fun addColdRebootTimes() {
        withContext(Dispatchers.Main) {
            _coldRebootTimes.value = _coldRebootTimes.value?.plus(1)
        }
        Log.e("TesterPage", "DSHelper.putData: ${COLD_NUM},${_coldRebootTimes.value}")
        DSHelper.putData(COLD_NUM, _coldRebootTimes.value ?: 0)
    }

    private fun addHotRebootTimes() {
        _hotRebootTimes.value = _hotRebootTimes.value?.plus(1)
    }

    suspend fun getBootType() {
        var typeInt = withContext(Dispatchers.IO) {
            DSHelper.getData(REBOOT_TYPE)
        }
        withContext(Dispatchers.Main) {
            type = when (typeInt) {
                TesterType.HOT_REBOOT.ordinal -> TesterType.HOT_REBOOT
                TesterType.COLD_REBOOT.ordinal -> TesterType.COLD_REBOOT
                else -> TesterType.HOT_REBOOT
            }
        }
    }

    suspend fun startHotReboot() {
        startTester()
        startReboot(TesterType.HOT_REBOOT)
    }

    suspend fun startColdReboot() {
        startTester()
        startReboot(TesterType.COLD_REBOOT)
    }

    private suspend fun startReboot(type: TesterType) {
        this.type = type
        DSHelper.putData(REBOOT_TYPE, type.ordinal)
    }

    suspend fun startTester() {
        withContext(Dispatchers.Main) {
            isTester.value = true
        }
        DSHelper.putData(IS_TESTER, 1)
    }

    suspend fun stopTester() {
        withContext(Dispatchers.Main) {
            isTester.value = false
            DSHelper.putData(IS_TESTER, 0)
        }
    }

    suspend fun checkIsTester() {
        var isTesterBool = withContext(Dispatchers.IO) {
            DSHelper.getData(IS_TESTER) == 1
        }
        withContext(Dispatchers.Main) {
            this@TestViewModel.isTester.value = isTesterBool
        }

    }

    var hotRebootTimes = _hotRebootTimes
        private set
    var maxHotRebootTimes = _maxHotRebootTimes
        private set
    var coldRebootTimes = _coldRebootTimes
        private set
    var maxColdRebootTimes = _maxColdRebootTimes
        private set


}

enum class TesterType(value: Int) {
    HOT_REBOOT(0),
    COLD_REBOOT(1)
}